package com.java2007.filters;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = {"/backend/*","/files/*","/front/*"})
public class LoginFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        //放行文件
        String uri =request.getRequestURI();
        System.out.println(uri);


        HttpSession session = request.getSession();
        Object admin = session.getAttribute("admin");
        if (admin == null){
            response.sendRedirect(request.getContextPath()+"/index.jsp");
            return;
        }
        chain.doFilter(request,response);
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
